﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using DoeLibsMVC.Models;
using DoeObjects;
using System.Web;
using DoeObjects;

namespace DoeLibsMVC.Controllers.api
{
    public class SearchController : ApiController
    {

        public SearchResult Get(string searchTerm, string searchOption)
        {
            if (!String.IsNullOrEmpty(searchTerm)) searchTerm = Uri.UnescapeDataString(searchTerm);
            searchOption = Uri.UnescapeDataString(searchOption);

            SearchResult searchResult = new SearchResult();

            if (searchOption == "Title") 
            {
                searchResult.ResultType = SearchResult.RESULT_TYPE.TITLE;
                searchResult.Titles = new List<TitleModel>();
                foreach (Title title in Title.getByTitle(searchTerm))
                {
                    searchResult.Titles.Add(createTitleModel(title));
                }

            }
            else if (searchOption == "Author") 
            {
				searchResult.ResultType = SearchResult.RESULT_TYPE.AUTHOR;
				searchResult.Authors = new List<AuthorModel>();
				List<Author> authors = new List<Author>();
				if (!String.IsNullOrEmpty(searchTerm)) authors = Author.getByName(searchTerm);
				else authors = Author.getAll();

				foreach (Author author in authors)
				{
					AuthorModel model = new AuthorModel();
					model.Author = author;
					model.Titles = Title.getTitlesFromAuthor(author.Aid);
					searchResult.Authors.Add(model);
				}
            }
			else if (searchOption == "Topic")
			{
				searchResult.ResultType = SearchResult.RESULT_TYPE.TOPIC;
				searchResult.Topics = new List<TopicModel>();
				List<Topic> topics = new List<Topic>();
				if (!String.IsNullOrEmpty(searchTerm)) topics = Topic.getByName(searchTerm, true);
				else topics = Topic.getAll();

				foreach (Topic topic in topics)
				{
					TopicModel model = new TopicModel();
					model.Topic = topic;
					model.Titles = Title.getTitlesFromTopic(topic.TopicId);
					searchResult.Topics.Add(model);
				}
            }
            else if (searchOption == "ISBN-10") 
            {
                searchResult.ResultType = SearchResult.RESULT_TYPE.TITLE;
                searchResult.Titles = new List<TitleModel>();
                foreach (Title title in Title.searchByIsbn10(searchTerm))
                {
                    searchResult.Titles.Add(createTitleModel(title));
                }   
            }
            else if (searchOption == "ISBN-13") 
            {
                searchResult.ResultType = SearchResult.RESULT_TYPE.TITLE;
                searchResult.Titles = new List<TitleModel>();
                foreach (Title title in Title.searchByIsbn13(searchTerm))
                {
                    searchResult.Titles.Add(createTitleModel(title));
                } 
            }

            return searchResult;
        }

        private TitleModel createTitleModel(Title title)
        {
            TitleModel tm = new TitleModel();
            tm.Title = title;
            tm.Loanables = Loanable.getLoanablesByTitle(title.TitleId);
            tm.Authors = Author.getAuthorsFromBook(title.TitleId);
            tm.Editors = Author.getEditorsFromBook(title.TitleId);
			tm.Topics = Topic.getTopicsFromBook(title.TitleId);

            return tm;
        }
    }
}
